using UnityEngine;
using System.Collections;

public class EncounterToFight : MonoBehaviour
{
	private static EncounterToFight instance;
	
	public static EncounterToFight GetInstance ()
	{
		if (!instance) {
			instance = (EncounterToFight)GameObject.FindObjectOfType (typeof(EncounterToFight));
		}
		
		return instance;
	}
	
	GameObject uiMain;
	GameObject mainRole;
	FightLogic fightLogic;
	int callLength;
	int callCount = 0;
	Camera mainCamera;
	Vector3[] posArray = new Vector3[]{new Vector3 (-150, -110, 0),new Vector3 (-150, -180, 0),new Vector3 (-150, -250, 0),
									new Vector3 (-250, -110, 0),new Vector3 (-250, -180, 0),new Vector3 (-250, -250, 0),
									new Vector3 (150, -110, 0),new Vector3 (150, -180, 0),new Vector3 (150, -250, 0),
									new Vector3 (250, -110, 0),new Vector3 (250, -180, 0),new Vector3 (250, -250, 0),
									new Vector3 (-380, -100, 0),new Vector3 (-380, -210, 0),
									new Vector3 (380, -100, 0),new Vector3 (380, -210, 0),
	};
	
	public Vector3 this [int index] {
		get {
			if (index < 0 || index > 16) {
				return Vector3.zero;
			} else {
				return posArray [index];
			}
		}
	}
	
	void Awake ()
	{
		fightLogic = gameObject.GetComponent<FightLogic> ();
		
		if (fightLogic == null) {
			gameObject.AddComponent<FightLogic> ();
		}
		
		fightLogic = gameObject.GetComponent<FightLogic> ();
		
		mainCamera = Camera.main;
	}
	
	public void SwitchToFightScene ()
	{
		uiMain = GameObject.Find ("Window_Main");
		if (uiMain != null) {
			uiMain.SetActive (false);
		}
		
		mainRole = PlayerData.Instance ().MainRoleInstance;
		
		if (mainRole != null) {
			mainRole.SetActive (false);
		}
		
		SmoothFollow smooth = mainCamera.GetComponent<SmoothFollow> ();
			
		if (smooth != null) {
			smooth.enabled = false;
		} else {
			Debug.LogError ("SmoothFollow fo Camera was not referenced");
		}
		
		MouseReplace mouse = mainCamera.GetComponent<MouseReplace> ();
		
		if (mouse != null) {
			mouse.enabled = false;
			mouse.mouse.gameObject.SetActive (false);
		} else {
			Debug.LogError ("MouseReplace fo Camera was not referenced");
		}
	}
	
	public void nextFight ()
	{
		callCount++;
		Debug.Log ("nextFight !!!");
		if (callCount != callLength)
			return;
		
		if (fightLogic != null) {
			callCount = 0;
			callLength = 0;
			fightLogic.FightInterface ();
		}
		Debug.Log ("nextFight ????");
	}
	
	public void SwitchToEncounter ()
	{
		uiMain.SetActive (true);
		
		mainRole.SetActive (true);
		
		SmoothFollow smooth = mainCamera.GetComponent<SmoothFollow> ();
		smooth.enabled = true;
		
		MouseReplace mouse = mainCamera.GetComponent<MouseReplace> ();
		mouse.enabled = true;
	}
	
	public void CallLength (int count)
	{
		callLength = count;
	}
	
	public void RemoveObj(GameObject go){
		fightLogic.RemoveObj(go);
	}
	
	public void ShowSkillName ()
	{
		
	}
	
	void OnGUI ()
	{
		if (GUI.Button (new Rect (500, 150, 100, 50), "callBack")) {
			fightLogic.startFight ();
		}
		
		if (GUI.Button (new Rect (600, 150, 100, 50), "PlayAgain")) {
			fightLogic.receiveFight ();
		}
	}
}
